package com.example.yunhailutong.zhongtan.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.yunhailutong.zhongtan.R;
import com.example.yunhailutong.zhongtan.app.App;
import com.example.yunhailutong.zhongtan.app.OldsEvent;
import com.example.yunhailutong.zhongtan.entity.DingDanDescEntity;
import com.example.yunhailutong.zhongtan.net.PersionBiz;
import com.zhy.adapter.abslistview.CommonAdapter;
import com.zhy.adapter.abslistview.ViewHolder;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by yunhailutong on 2018/4/26.
 */

public class ActivityDingDanDesc extends ActivityBase {


    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.bianhao1)
    TextView bianhao1;
    @BindView(R.id.bianhao)
    TextView bianhao;
    @BindView(R.id.state)
    TextView state;
    @BindView(R.id.chepaihao)
    TextView chepaihao;
    @BindView(R.id.gongsimingcheng)
    TextView gongsimingcheng;
    @BindView(R.id.listview)
    ListView listview;
    ArrayList<String> lists = new ArrayList<>();
    @BindView(R.id.text1)
    TextView text1;
    @BindView(R.id.text2)
    TextView text2;
    @BindView(R.id.text3)
    TextView text3;
    @BindView(R.id.text4)
    TextView text4;
    @BindView(R.id.submit)
    Button submit;
    @BindView(R.id.queren)
    Button queren;
    @BindView(R.id.quxiao)
    Button quxiao;

    private String states = "";


    @Override
    protected void addListener() {
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    @Override
    protected void initActivity(Bundle savedInstanceState) {
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
    }

    @Override
    protected int contentViewById() {
        return R.layout.activity_dingdan_desc;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        Map<String, String> params = new HashMap<>();
        params.put("gd", getIntent().getStringExtra("id"));
        params.put("phone",  App.number);
        params.put("token", App.token);
        new PersionBiz().GetDingDanDesc(params);
        states = getIntent().getStringExtra("state");
        showWaitingDialog();
    }

    @Subscribe
    public void log(OldsEvent.DingDanDescEvent msg) {
        dismissWaitingDialog();
        if (msg.what == -1) {
            disPlay(msg.obj.toString());
        }
        if (msg.what == 1) {
            DingDanDescEntity entity = (DingDanDescEntity) msg.obj;
            if (entity.getData().get(0).getOrderDate() != null) {
                lists.add("订单创建：" + entity.getData().get(0).getOrderDate());
                lists.add("货物发出：" + entity.getData().get(0).getOrderDate() + "  " + entity.getData().get(0).getCarNo());
            }
            if (entity.getData().get(0).getTestDate() != null) {
                lists.add("质检完成：" + entity.getData().get(0).getTestDate());
                if (entity.getData().get(0).getPayDate() != null) {
                    lists.add("结算完成：" + entity.getData().get(0).getPayDate());
                } else {
                    lists.add("结算未完成请等待");
                }
            } else if(entity.getData().get(0).getQuaityDate() != null){
                lists.add("质检完成：" + entity.getData().get(0).getTestDate());
                if (entity.getData().get(0).getPayDate() != null) {
                    lists.add("结算完成：" + entity.getData().get(0).getPayDate());
                } else {
                    lists.add("结算未完成请等待");
                }
            }else{
                lists.add("待质检");
            }

            initView(entity);
        }
    }
    @Subscribe
    public void logs(OldsEvent.ConFirmeDingDanEvent msg) {
        dismissWaitingDialog();
        if (msg.what == -1) {
            disPlay(msg.obj.toString());
        }
        if (msg.what == 1) {
           disPlay("提交成功");
           finish();
        }
    }
    private void initView(DingDanDescEntity entity) {

        state.setText(getIntent().getStringExtra("state"));
        listview.setAdapter(new CommonAdapter<String>(getActivity(), R.layout.item_dingdan_desc, lists) {
            @Override
            protected void convert(ViewHolder viewHolder, String item, int position) {
                viewHolder.setText(R.id.tx_action, item);
//                if(item.equals("待质检")){
//                    viewHolder.setTextColor(R.id.tx_action,R.color.green);
//                }
            }
        });
        if (states.equals("待质检")) {
            submit.setVisibility(View.VISIBLE);
            submit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(getActivity(), ActivityNewDingDan.class);
                    intent.putExtra("id", getIntent().getStringExtra("id"));
                    startActivity(intent);
                }
            });
        } else if (states.equals("待确认")) {
            submit.setVisibility(View.GONE);
            queren.setVisibility(View.VISIBLE);
            quxiao.setVisibility(View.VISIBLE);
            queren.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Map<String, String> params = new HashMap<>();
                    params.put("oi_id", getIntent().getStringExtra("id"));
                    params.put("oi_confirmStatus", "1");
                    params.put("token", App.token);
                    params.put("phone", App.number);
                    new PersionBiz().confirmOrder(params);
                    showWaitingDialog();
                }
            });
            quxiao.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Map<String, String> params = new HashMap<>();
                    params.put("oi_id", getIntent().getStringExtra("id"));
                    params.put("oi_confirmStatus", "2");
                    params.put("token", App.token);
                    params.put("phone", App.number);
                    new PersionBiz().confirmOrder(params);
                    showWaitingDialog();
                }
            });

        }else{

        }
        listview.setDivider(null);
        bianhao.setText(entity.getData().get(0).getOrderNo());
        chepaihao.setText("车牌号：" + entity.getData().get(0).getCarNo());
        gongsimingcheng.setText(entity.getData().get(0).getFname());
        text1.setText(entity.getData().get(0).getPname());
        text2.setText(entity.getData().get(0).getTotalAmount() + "吨");
        if (entity.getData().get(0).getLevel() != null) {
            text3.setText(entity.getData().get(0).getLevel() + "级");
        }
    }
}
